<?php
include './include/common.inc.php';
$action = isset($action) ? $action : 'manage';
$forward = get_forward('/admin/site_conf.php');
$list = $site_conf->get_site_conf();
if(isset($_GET['site_id']) && !empty($_GET['site_id']) && ($_GET['site_id']!=$list['SITE_ID']) && ($list['IS_MASTER']==1) )
{
	$file = APP_ROOT . 'sites/' . $_GET['site_id'] . '/site.conf';
	$m_site_conf = new siteconf($file);
	$list = $m_site_conf->get_site_conf();
}
else 
{
	$m_site_conf = $site_conf;
}

switch ($action)
{
	case 'add':
		$have_perm = check_perms('siteconf_add');
		
		if(isset($_POST['value']))
		{
			if($have_perm == false)
			{
				$response->alert('您没有权限进行此操作！');
				$response->execute();
			}
			if(empty($_POST['key']))
			{
				$response->text_alert('error','key','请填写配置的键');
			}
			if(empty($_POST['value']))
			{
				$response->text_alert('error','value','请填写配置的值');
			}
			$response->real_execute();
			$res = $m_site_conf->set_site_conf($_POST['key'], $_POST['value']);
			if($res === true)
			{
				$response->alert('添加成功！');
				$response->redirect($forward);
			}
			$response->execute();
		}
		else 
		{
			if($have_perm == false)
			{
				showmessage('您没有权限进行此操作！');
			}
			include template('admin', 'site_conf_add');
		}
		break;
	case 'edit':
		$have_perm = check_perms('siteconf_edit');
		if($have_perm == false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->execute();
		}
		if(isset($_POST['value']) && is_array($_POST['value']))
		{
			foreach ($_POST['value'] as $k=>$v)
			{
				$_value = is_array($v) ? implode(',',$v) : $v;
				$m_site_conf->set_site_conf($k, $_value);
			}
		}
		$response->alert('修改完毕！');
		$response->script('self.location=self.location');
		$response->execute();
		break;
	case 'del':
		$have_perm = check_perms('siteconf_del');
		if($have_perm == false)
		{
			$response->alert('您没有权限进行此操作！');
			$response->execute();
		}
		$m_site_conf->del_site_conf($_GET['key']);
		$response->alert('删除完毕！');
		$response->script('self.location=self.location');
		$response->execute();
		break;
	default:
		$have_perm = check_perms('siteconf_view');
		if($have_perm == false)
		{
			showmessage('您没有权限进行此操作！');
		}
		
		$forward = make_forward();
		include template('admin', 'site_conf');
}
?>